package com.ztesoft.report.engine.query;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


import com.ztesoft.report.engine.data.ReportDataSource;
import com.ztesoft.report.engine.data.ReportParameter;
import org.apache.commons.lang3.StringUtils;

/**
 * @author tomdeng
 */
public class MySqlQueryer extends AbstractQueryer implements Queryer {
    public MySqlQueryer(final ReportDataSource dataSource, final ReportParameter parameter) {
        super(dataSource, parameter);
    }

/*    @Override
    protected String preprocessSqlText(String sqlText) {
        sqlText = StringUtils.stripEnd(sqlText.trim(),"");
        final Pattern pattern = Pattern.compile("limit.*?$", Pattern.CASE_INSENSITIVE);
        final Matcher matcher = pattern.matcher(sqlText);
        if (matcher.find()) {
            sqlText = matcher.replaceFirst("");
        }
        return sqlText + " limit 1";
    }*/

    @Override
    protected String preprocessSqlText(String sqlText) {
        sqlText = StringUtils.lowerCase(sqlText);
        if ("rownum".contains(sqlText)) {
            return sqlText;
        } else {
            return "select * from (" + sqlText + ") where rownum = 1";
        }
    }
}
